package com.recsmile.sgs.ui.components;

import com.recsmile.sgs.listener.HealthListener;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Administrator on 2016/8/25.
 */
public class LifebarGui extends JPanel implements HealthListener{
    public static final int WIDTH = 50;
    public static final int HEIGHT = CardRackGui.HEIGHT;
    private int limit;
    private int current;

    public LifebarGui() {
        setSize(WIDTH, HEIGHT);
        setLocation(GamePanelGui.WIDTH - WIDTH, GamePanelGui.HEIGHT - HEIGHT);
        setLayout(null);
        limit = 0;
        current = 0;
    }

    @Override
    public synchronized void onSetHealthLimit(int limit) {
        this.limit = limit;
        repaint();
    }

    @Override
    public synchronized void onSetHealthCurrent(int current) {
        this.current = current;
        repaint();
    }

    @Override
    public synchronized void onHealthChangedBy(int amount) {
        current += amount;
        if (current > limit)// may not be necessary
            current = limit;
        repaint();
    }

    @Override
    public synchronized void onDeath() {

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawRect(0, 0, WIDTH, HEIGHT);
        if (limit == 0)
            return;
        int diameter = HEIGHT / (limit * 2 + 1);
        int x = (WIDTH - diameter) / 2;
        for (int i = 0; i < limit; i++) {
            if (i < current)
                g.setColor(Color.GREEN);
            else
                g.setColor(Color.RED);
            g.fillOval(x, (2 * i + 1) * diameter, diameter, diameter);
        }
    }
}
